#include "startwizard.h"

startwizard::startwizard(QWidget *parent) : QWizard(parent)
{

}

startwizard::startwizard(StaticMain &GlobalSettings, QWidget *parent) : QWizard(parent)
{
    this->GlobalSettings = GlobalSettings;
    setPage(Page_Intro, new pr_intro(GlobalSettings, this));
    setPage(Page_Load, new pr_load(GlobalSettings, this));
    setPage(Page_Participants, new pr_participants(GlobalSettings, this));
    setPage(Page_Type, new pr_type(GlobalSettings, this));
    setPage(Page_Sample, new pr_sample(GlobalSettings, this));
    setPage(Page_Finalization, new pr_finalize(GlobalSettings, this));
    this->show();
}

int startwizard::nextId() const
{
    switch (currentId()) {
    case Page_Intro:
        /*if (field("intro.evaluate").toBool()) {
            return Page_Load;
        } else {*/
            return Page_Participants;
        //}
    case Page_Load:
        return Page_Finalization;
    case Page_Sample:
        return Page_Finalization;
    case Page_Finalization:
        return -1;
    default:
        return currentId()+1;
    }
}


pr_intro::pr_intro(StaticMain &GlobalSettings, QWidget *parent) : QWizardPage(parent)
{
    this->GlobalSettings = GlobalSettings;
    this->setTitle("Load or create project");

    QVBoxLayout *layout = new QVBoxLayout();

    QLabel *label1 = new QLabel("This wizard will help you to"
                               "(pre-)set the experimental conditions and the program.");
    label1->setWordWrap(true);

    QLabel *label2 = new QLabel("Do you want to continue an existing project or start a new one?");
    label2->setWordWrap(true);

    QRadioButton *rb1 = new QRadioButton("&Continue existing project");
    QRadioButton *rb2 = new QRadioButton("&Create new project");

    this->registerField("ProjectNew", rb2);

    rb1->setChecked(true);

    layout->addWidget(label1);
    layout->addWidget(label2);
    layout->addWidget(rb1);
    layout->addWidget(rb2);

    this->setLayout(layout);
}

pr_load::pr_load(StaticMain &GlobalSettings, QWidget *parent) : QWizardPage(parent)
{
    this->GlobalSettings = GlobalSettings;
    this->setTitle("Load project");

    QVBoxLayout *layout = new QVBoxLayout();

    QLabel *label = new QLabel("Please select the project you want to continue.");
    label->setWordWrap(true);
    layout->addWidget(label);

    this->setLayout(layout);
}


pr_participants::pr_participants(StaticMain &GlobalSettings, QWidget *parent) : QWizardPage(parent)
{
    this->GlobalSettings = GlobalSettings;
    this->setTitle("Register participants");

    QLabel *label = new QLabel("Please add participants to your project."
                               "These should be all persons that are using this program during your project."
                               "\nParticipants can be added or removed at any time.");
    label->setWordWrap(true);

    this->LVregisteredPhysicists = new QListView();
    this->LVparticipants = new QListView();

    this->LVregisteredPhysicists->setSelectionBehavior(QAbstractItemView::SelectItems);
    this->LVregisteredPhysicists->setSelectionBehavior(QAbstractItemView::SelectItems);

    this->LVregisteredPhysicists->setSelectionMode(QAbstractItemView::ExtendedSelection);
    this->LVparticipants->setSelectionMode(QAbstractItemView::ExtendedSelection);

    QPushButton *bAdd = new QPushButton();
    bAdd->setText("+");
    connect(bAdd,
            SIGNAL(clicked()),
            this,
            SLOT(AddRemovePhysicists())
            );

    QPushButton *bRemove = new QPushButton();
    bRemove->setText("-");
    connect(bRemove,
            SIGNAL(clicked()),
            this,
            SLOT(AddRemovePhysicists())
            );

    QPushButton *bAddAll = new QPushButton();
    bAddAll->setText(">>");
    connect(bAddAll,
            SIGNAL(clicked()),
            this,
            SLOT(UnRegisterPhysicists())
            );

    QPushButton *bAddOne = new QPushButton();
    bAddOne->setText(">");
    connect(bAddOne,
            SIGNAL(clicked()),
            this,
            SLOT(UnRegisterPhysicists())
            );

    QPushButton *bRemOne = new QPushButton();
    bRemOne->setText("<");
    connect(bRemOne,
            SIGNAL(clicked()),
            this,
            SLOT(UnRegisterPhysicists())
            );

    QPushButton *bRemAll = new QPushButton();
    bRemAll->setText("<<");
    connect(bRemAll,
            SIGNAL(clicked()),
            this,
            SLOT(UnRegisterPhysicists())
            );

    QGridLayout* layout = new QGridLayout();

    layout->addWidget(label,0,0,1,9);
    layout->addWidget(LVregisteredPhysicists,1,0,7,5);
    layout->addWidget(LVparticipants,1,9,7,1);
    layout->addWidget(bAddAll,1,7,1,1);
    layout->addWidget(bAddOne,3,7,1,1);
    layout->addWidget(bRemOne,5,7,1,1);
    layout->addWidget(bRemAll,7,7,1,1);

    layout->addWidget(bAdd,9,1,1,1);
    layout->addWidget(bRemove,9,3,1,1);

    this->setLayout(layout);

    UpdateLVPhys();
}

pr_type::pr_type(StaticMain &GlobalSettings, QWidget *parent) : QWizardPage(parent)
{
    this->GlobalSettings = GlobalSettings;
    this->setTitle("Select experiment");

    QVBoxLayout *layout = new QVBoxLayout();

    QLabel *label = new QLabel("Please choose, what kind of experiment you want to do."
                               "\nThe type can be changed at any time.");
    label->setWordWrap(true);

    QRadioButton *rb1 = new QRadioButton("&Angle-Resolved");
    QRadioButton *rb2 = new QRadioButton("&Time-Resolved");
    QRadioButton *rb3 = new QRadioButton("Angle- && Time-&Resolved");

    layout->addWidget(label);
    layout->addWidget(rb1);
    layout->addWidget(rb2);
    layout->addWidget(rb3);

    this->setLayout(layout);
}

pr_sample::pr_sample(StaticMain &GlobalSettings, QWidget *parent) : QWizardPage(parent)
{
    this->GlobalSettings = GlobalSettings;
    this->setTitle("Select sample");

    QVBoxLayout *layout = new QVBoxLayout();

    QLabel *label = new QLabel("Please select the current sample."
                               "\nThe sample can be changed at any time.");
    label->setWordWrap(true);


    QHBoxLayout *listboxlayout = new QHBoxLayout();

    QListView *LVregisteredSamples = new QListView();

    QStandardItemModel *MregSamp = new QStandardItemModel();
    LVregisteredSamples->setModel(MregSamp);

    foreach (sample regSample, this->GlobalSettings.GetRegSamples())
    {
        QStandardItem *item = new QStandardItem();
        item->setData( regSample.GetName(), Qt::DisplayRole );
        item->setEditable( false );
        MregSamp->appendRow( item );
    }
    LVregisteredSamples->setSelectionBehavior(QAbstractItemView::SelectItems);

    listboxlayout->addWidget(LVregisteredSamples);

    layout->addWidget(label);
    layout->addLayout(listboxlayout);

    this->setLayout(layout);

}

pr_finalize::pr_finalize(StaticMain &GlobalSettings, QWidget *parent) : QWizardPage(parent)
{
    this->GlobalSettings = GlobalSettings;
    this->setTitle("Finalize project");

    QVBoxLayout *layout = new QVBoxLayout();

    QLabel *label = new QLabel("Please check if everything is set correctly.");
    label->setWordWrap(true);
    layout->addWidget(label);

    this->setLayout(layout);
}


void pr_participants::UnRegisterPhysicists()
{

    QStringList ButtonTexts;
    ButtonTexts << "<<" << "<" << ">" << ">>";
    QPushButton* sender = ((QPushButton*)QObject::sender());

    switch (ButtonTexts.indexOf(sender->text()))
    {
    case 0:
        this->GlobalSettings.currentproject.removeParticipant();
        break;
    case 1:
        foreach (QModelIndex currentindex, this->LVparticipants->selectionModel()->selectedIndexes())
        {
            int physindex = currentindex.sibling(currentindex.row(),0).data().toInt();
            physicist remPart = GlobalSettings.GetRegPhys(physindex);
            this->GlobalSettings.currentproject.removeParticipant(remPart);
        }
        break;
    case 2:
        foreach (QModelIndex currentindex, this->LVregisteredPhysicists->selectionModel()->selectedIndexes())
        {
            int physindex = currentindex.sibling(currentindex.row(),0).data().toInt();
            physicist newPhys = GlobalSettings.GetRegPhys(physindex);
            this->GlobalSettings.currentproject.addParticipant(newPhys);
        }
        break;
    case 3:
        this->GlobalSettings.currentproject.addParticipant(GlobalSettings.GetRegPhysList());
        break;
    }
    UpdateLVPhys();

}

void pr_participants::UpdateLVPhys()
{
    QList<physicist> regPhys = this->GlobalSettings.GetRegPhysList();
    //QList<physicist> participants = StaticMain::GetCurrentProject().getParticipants();

    QStandardItemModel *MregPhys = new QStandardItemModel();
    QStandardItemModel *Mparticipants = new QStandardItemModel();

    QStringList header;
    header << "ID" << "Name";

    (*MregPhys).setHorizontalHeaderLabels(header);
    (*Mparticipants).setHorizontalHeaderLabels(header);

    project currentproject = this->GlobalSettings.currentproject;

    foreach (physicist phys, regPhys)
    {
        QStandardItem *itemname = new QStandardItem();
        (*itemname).setData( phys.GetName(), Qt::DisplayRole );
        (*itemname).setEditable( false );

        QStandardItem *itemid = new QStandardItem();
        (*itemid).setData( phys.GetID(), Qt::DisplayRole );
        (*itemid).setEditable( false );

        QList<QStandardItem*> newPhys;
        newPhys.append(itemid);
        newPhys.append(itemname);

        if(currentproject.isParticipant(phys))
            Mparticipants->appendRow( newPhys );
        else
            MregPhys->appendRow( newPhys );
    }

    this->LVregisteredPhysicists->setModel(MregPhys);
    this->LVparticipants->setModel(Mparticipants);

    this->LVregisteredPhysicists->setModelColumn(1);
    this->LVparticipants->setModelColumn(1);
}

void pr_participants::AddRemovePhysicists()
{
    QStringList ButtonTexts;
    ButtonTexts << "-" << "+";
    QPushButton* sender = ((QPushButton*)QObject::sender());

    if (ButtonTexts.indexOf(sender->text()))
        this->GlobalSettings.AddPhysicists("test1", "test2", "Dynamics");
    UpdateLVPhys();
}
